package com.yyy.gui.pannel;

import com.yyy.entity.Category;
import com.yyy.gui.listener.CategoryListener;
import com.yyy.gui.model.CategoryTableModel;
import com.yyy.service.CategoryService;
import com.yyy.util.ColorUtil;
import com.yyy.util.GUIUtil;

import javax.swing.*;
import java.awt.*;

/**
 * @Author：yyy
 * @Package：com.yyy.gui.pannel
 * @Project：HuTuBiill
 * @name：CategoryPanel
 * @Date：2024/11/9 21:25
 * @Filename：CategoryPanel
 * 分类面板
 */
public class CategoryPanel extends WorkingPanel{
    public static CategoryPanel instance=new CategoryPanel();
    public JButton bAdd=new JButton("新增");
    public JButton bEdit=new JButton("编辑");
    public JButton bDelete=new JButton("删除");
    String[] columNames={"分类名称","消费次数"};
    public CategoryTableModel ctm=new CategoryTableModel();
    public JTable t=new JTable(ctm);
    public CategoryPanel(){
        GUIUtil.setColor(ColorUtil.blueColor,bAdd,bEdit,bDelete);
        JScrollPane sp=new JScrollPane(t);
        JPanel pSubmit=new JPanel();
        pSubmit.add(bAdd);
        pSubmit.add(bEdit);
        pSubmit.add(bDelete);
        this.setLayout(new BorderLayout());
        this.add(sp,BorderLayout.CENTER);
        this.add(pSubmit,BorderLayout.SOUTH);
        addListener();
    }

    public Category getSelectedCategory(){
        int index=t.getSelectedRow();
        return ctm.cs.get(index);
    }



    @Override
    public void addListener() {
        CategoryListener categoryListener=new CategoryListener();
        bAdd.addActionListener(categoryListener);
        bEdit.addActionListener(categoryListener);
        bDelete.addActionListener(categoryListener);

    }

    @Override
    public void updateData() {
        ctm.cs=new CategoryService().list();
        t.updateUI();
        t.getSelectionModel().setSelectionInterval(0,0);//默认选中第一行信息
        if(ctm.cs.size()==0){
            bEdit.setEnabled(false);
            bDelete.setEnabled(false);
        }else {
            bEdit.setEnabled(true);
            bDelete.setEnabled(true);
        }
    }


}
